<?php

namespace pc\models;

use common\helpers\Helper;
use common\Service\RedisService;
use Yii;
use common\models\Banner as P_Banner;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;


class Banner extends P_Banner
{
    const BANNER_REDIS_KEY = 'PcBanners';
    private $banners = [];
    private $fields = ['id','title','image','desc','link'];  //banner必须字段
    private $maxSaveBannerNumber = 10;  //最多保存10条最新的banner

    public function init()
    {
        $this->banners = RedisService::getJson(self::BANNER_REDIS_KEY);

        //没有banner的时候 随机来几个写真作为banner
        if(empty($this->banners)) $this->randomBanners();
    }

    private function randomBanners(){
        $portrays = Portray::randomPortrays(3,['id','title','desc','x_img']);
        foreach ($portrays as $portray){
            $this->addPortrayBanner($portray['id'],$portray);
        }
    }

    /*
     * 对象方式获取banner
     */
    public function getBanners($limit = 103){
        return $this->banners ? array_slice($this->banners,0,$limit) : [];
    }

    /*
     * 静态方式获取banner 不用实例化对象
     */
    public static function getBannersByStatic($limit = 3){
        $banners = RedisService::getJson(self::BANNER_REDIS_KEY);
        return $banners ? array_slice($banners,0,$limit) : [];
    }

    /*
     * 添加新闻banner
     */
    public function addNewsBanner($newId = 0,$newInfo = []){
        empty($newInfo) && $newInfo = News::getNewsInfoById($newId,['id','title','image','desc']);

        if(!empty($newInfo)){
            $newInfo['link'] = "/news/{$newInfo['id']}.html";
            $newInfo['image'] = Helper::parseImg($newInfo['image']);
            $newInfo['unicode'] = "new:{$newInfo['id']}";

            $this->addBanner("news:{$newInfo['id']}",$newInfo);
        }
    }

    /*
     * 添加写真banner
     */
    public function addPortrayBanner($portrayId = 0,$portrayInfo =[]){
        empty($portrayInfo) && $portrayInfo = Portray::getPortrayById($portrayId,['id','title','desc']);

        if(!empty($portrayInfo)){
            $portrayInfo['link'] = "/xiezhen/{$portrayInfo['id']}.html";
            $portrayInfo['image'] = $portrayInfo['x_img'];

            $portrayInfo['image'] = Helper::parseImg($portrayInfo['image']);
            $portrayInfo['unicode'] = "portray:{$portrayInfo['id']}";
            $this->addBanner("portray:{$portrayInfo['id']}",$portrayInfo);
        }
    }

    /*
     * 添加标准格式的banner
     */
    public function addBanner($bannerId,$banner){
        $rs = true;

        $tmp = [];
        foreach ($this->fields as $field){
            if(isset($banner[$field]) && !empty($banner[$field])){
                $tmp[$field] = $banner[$field];
            }else{
                $rs = false;
                break;
            }
        }

        if($rs){
            if(empty($this->banners)){
                $this->banners[$bannerId] = $tmp;
            }else{

                $bannerKeys = array_keys($this->banners);
                if(in_array($bannerId,$bannerKeys)){
                    unset($this->banners[$bannerId]);
                    $_key = array_search($bannerId,$bannerKeys);
                    if($_key !== false) unset($bannerKeys[$_key]);
                }

                if(!empty($bannerKeys)){
                    $bannerKeys = array_reverse($bannerKeys);

                    $result = [];
                    $result[$bannerId] = $tmp;
                    foreach ($bannerKeys as $vo){
                        $result[$vo] = $this->banners[$vo];
                    }
                    $this->banners = $result;
                }else{
                    $this->banners[$bannerId] = $tmp;
                }

            }

            RedisService::setJsonString(self::BANNER_REDIS_KEY,$this->banners);
        }

        return $rs;
    }
}
